﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace DR5.SPExtend
{
    public static class HashtableExtensions
    {
        public static DataTable ToDataTable(this Hashtable hashtable)
        {
            var dataTable = new DataTable(hashtable.GetType().Name);
            dataTable.Columns.Add(StringEx.Key, typeof(object));
            dataTable.Columns.Add(StringEx.Value, typeof(object));

            foreach (DictionaryEntry var in hashtable)
            {
                dataTable.Rows.Add(var.Key, var.Value);
            }
            return dataTable;
        }

        /// <summary>
        /// Hastable object are converted as string and returned as DataTable 
        /// </summary>
        /// <param name="hashtable"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static DataTable ToDataTableAllString(this Hashtable hashtable, string tableName)
        {
            var dataTable = new DataTable(tableName);
            dataTable.Columns.Add(StringEx.Key, typeof(string));
            dataTable.Columns.Add(StringEx.Value, typeof(string));

            foreach (DictionaryEntry var in hashtable)
            {
                dataTable.Rows.Add(var.Key.ToString() + " : " + var.Key.GetType().FullName, var.Value.ToString() + " : " + var.Value.GetType().FullName);
            }
            return dataTable;
        }

        public static Hashtable AddValue(this Hashtable hashtable, object key, object value)
        {
            hashtable[key] = value;
            return hashtable;
        }
    }
}
